package com.icinema.server;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.icinema.client.Constants;
import com.icinema.client.UserService;
import com.icinema.vo.User;

public class UserServiceImpl extends RemoteServiceServlet implements UserService {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4686674254224024164L;
	DatastoreService datastore;
	public UserServiceImpl(){
		datastore = DatastoreServiceFactory.getDatastoreService();	
	}
	public String addUser(User user){
		Query query = new Query(User.class.getSimpleName());
		query.addFilter("email", FilterOperator.EQUAL, user.getEmail());
		Iterable<Entity> itr = datastore.prepare(query).asIterable(FetchOptions.Builder.withDefaults());
		if(itr.iterator().hasNext()){
			return KeyFactory.keyToString(itr.iterator().next().getKey());
		}
		Entity entity = new Entity(User.class.getSimpleName());
		entity.setProperty("email",user.getEmail());
		entity.setProperty("password",user.getPassword());
		entity.setProperty("status",user.getStatus());
		datastore.put(entity);
		System.out.println("Context Path:" + getThreadLocalRequest().getContextPath());
		return KeyFactory.keyToString(entity.getKey());
	}
	public void sendEmail(String userEmail,String userKey){
		Email email = new Email();
		List<String> toList = new ArrayList<String>();
		toList.add(userEmail);
		email.sendMail("zawhtutwin@gmail.com",toList,"Confirmation Email from ICinema", getContent(userKey));
	}
	private String getContent(String userKey){
		String content = 
				"<html xmlns='http://www.w3.org/1999/xhtml'>"+
				"<head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"+
				"<title>Untitled Document</title>"+
				"<style type='text/css'>"+
				
				"body p {font-family: 'Courier New', Courier, monospace;"+
				"}"+
				"body p {"+
				"	font-family: Arial, Helvetica, sans-serif;"+
				"}"+
				"</style>"+
				"</head>"+
				"<body>"+
				"<p>Thank you for registering with us. Kindly click <a href='http://i-cinema.appspot.com/icinema/confirm.do'>here</a> to  confirm the address.</p>"+
				"<p>If the above link doesn't work, use the following url to confirm your registration.</p>"+
				"<p><a href='http://i-cinema.appspot.com/icinema/confirm.do?selected="+userKey+"'>http://i-cinema.appspot.com/icinema/confirm.do?selected="+userKey+"</a></p>"+
				"<p>Thanks and Regards,</p>"+
				"<p>I-Cinema</p>"+
				"</body>"+
				"</html>";
		System.out.println(userKey);
		return content;
	}
	
	@Override
	public User getUser(User filter) {
		Query query = new Query(User.class.getSimpleName());
		query.addFilter("email", FilterOperator.EQUAL, filter.getEmail());
		query.addFilter("password", FilterOperator.EQUAL, filter.getPassword());
		query.addFilter("status", FilterOperator.EQUAL, "A");
		Iterable<Entity> itr = datastore.prepare(query).asIterable(FetchOptions.Builder.withDefaults());
		for(Entity entity:itr){
				System.out.println(filter.getEmail() + "is valid user");
				getThreadLocalRequest().getSession().setAttribute(Constants.SESSION_USER_ID,KeyFactory.keyToString(entity.getKey()));				
				return fromUserEntity(entity); 
		}

		return null;
	}
	public User fromUserEntity(Entity entity){
		User user = new User();
		user.setId(KeyFactory.keyToString(entity.getKey()));
		user.setEmail((String)entity.getProperty("email"));
		user.setPassword((String)entity.getProperty("email"));
		user.setStatus((String)entity.getProperty("status"));
		return user;
	}
}
